<?php
/**
 * Created by PhpStorm.
 * User: soft
 * Date: 2020-6-11
 * Time: 9:55
 */

namespace app\admin\model;


use think\Model;

class Outsideschool extends Model
{
    // 表名
    protected $name = 'outsideschool';

    const is_check = [0 => '待审核', 1 => '审核通过', 2 => '已驳回'];
    const is_use = [0 => '未进入', 1 => '已进入'];
    const gate = [1 => '东门', 2 => '南门', 3 => '西门', 4 => '蓝谷校区', 5=>'东北门',11=>'平度校区'];

    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;


    protected $append = [
        'create_time_text',
        'use_time_text',
        'check_time_text',
        'is_check_text',
        'is_use_text',
        'admission_time',
        'admission_starttime_text',
        'admission_endtime_text',
        'use_record_text',
    ];
    public function getUseRecordTextAttr($value, $data){
        if (isset($data['use_time_record']) ){
            $use_record_count = count(explode(',',$data['use_time_record']));
            return $use_record_count;
        }
    }


    public function getAdmissionStarttimeTextAttr($value, $data){
        return date('Y-m-d', strtotime($data['admission_starttime']));
    }

    public function getAdmissionEndtimeTextAttr($value, $data){
        return date('Y-m-d', strtotime($data['admission_endtime']));
    }


    public function getCreateTimeTextAttr($value, $data){
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getUseTimeTextAttr($value, $data){
        $value = $value ? $value : (isset($data['use_time']) ? $data['use_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }
    public function getCheckTimeTextAttr($value, $data){
        $value = $value ? $value : (isset($data['check_time']) ? $data['check_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }

    public function getIsCheckTextAttr($value, $data){
        $value = $value ? $value : (isset($data['is_check']) ? $data['is_check'] : '00');
        return isset(self::is_check[$value]) ? self::is_check[$value] : '';
    }

    public function getIsUseTextAttr($value, $data){
        $value = $value ? $value : (isset($data['is_use']) ? $data['is_use'] : '00');
        return isset(self::is_use[$value]) ? self::is_use[$value] : '';
    }

    public function getAdmissionTimeAttr($value, $data){
        if (isset($data['admission_starttime']) && isset($data['admission_endtime'])){
            if ($data['admission_endtime'] == $data['admission_starttime']){
                return date('Y-m-d', strtotime($data['admission_starttime']));
            } else{
                return date('Y-m-d', strtotime($data['admission_starttime'])) . ' ~ ' . date('Y-m-d', strtotime($data['admission_endtime']));
            }
        }
    }

}